package com.cloud.point.concurrent.general;

import lombok.extern.slf4j.Slf4j;

/**
 * ThreadLocal使用
 * - 线程的局部变量
 *
 * @author 24373
 * @version 1.0
 * @date 2024-08-03 17:13
 */
@Slf4j
public class TestThreadLocal {

    private static final ThreadLocal<String> CLASS_LOCAL = new ThreadLocal<>();
    //继承类threadLocal ；现成工具阿里开源 ttl
    private static final ThreadLocal<String> CLASS_LOCAL2 = new InheritableThreadLocal<>();

    public static void main(String[] args) {

        new Thread(() -> {
            CLASS_LOCAL.set("b1");
            CLASS_LOCAL2.set("b2");
            log.info("类线程{}的值：父线程值：{}，子线程值：{}", Thread.currentThread().getName(), CLASS_LOCAL.get(), CLASS_LOCAL2.get());
            new Thread(() -> {
                String str2 = CLASS_LOCAL.get();
                String str3 = CLASS_LOCAL2.get();
                log.info("类线程{}的值：父线程值：{}，子线程值：{}", Thread.currentThread().getName(), str2, str3);
            }, "t1子线程").start();
        }, "t1").start();
        String s2 = CLASS_LOCAL.get();
        log.info("类线程{}的值：{}", Thread.currentThread().getName(), s2);

    }

}
